<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Software extends Base_Controller {
    
    public $articlesum = 0;
    public $limit = 5;
    public $page = 0;

    public function add(){
        $this->load->view('public/head');
        $this->load->view('pages/addsoftware');
    }
    public function lists(){
        
        $this->page = $this->uri->segment(3);
        $query = $this->db->select('*')->order_by('id DESC')->get('qbjh_software');
        $list = $query->result_array();
        $this->articlesum = count($list);

        $this->load->library('pagination');
        $config['base_url'] = site_url('software/lists');
        $config['first_link'] = '首页';
        $config['last_link'] = '末页';
        $config['next_link'] = '>>';
        $config['cur_tag_open'] = '<span class="current">';
        $config['cur_tag_close'] = '</span>';
        $config['prev_link'] = '<<';
        $config['total_rows'] = $this->articlesum;
        $config['per_page'] = $this->limit;
        $this->pagination->initialize($config);
        $this->data['currentpage'] = $this->page;
        $query = $this->db->select('*')->order_by('id DESC')->limit($this->limit, $this->page)->get('qbjh_software');
        $this->data['softwarelist'] = $query->result_array();
        $this->load->view('public/head', $this->data);
        $this->load->view('pages/softwarelist');
    }
    
    public function adding(){
        
        $redata = $this->httpcode;
        $name = $this->input->post('name');
        $score = $this->input->post('score');
        $cate = $this->input->post('cate');
        $lottery = $this->input->post('lottery');
        $os = $this->input->post('os');
        $download = $this->input->post('download');
        $help = $this->input->post('help');
        $thumb = $this->input->post('thumb');
        
        $software = [
            'name'=>$name,
            'score'=>$score,
            'cate'=>$cate,
            'lottery'=>$lottery,
            'os'=>$os,
            'download'=>'uploads/software/'.$download,
            'help'=>$help,
            'thumb'=>'uploads/media/'.$thumb
        ];
        $this->db->insert('qbjh_software',$software);
        if($this->db->affected_rows()){
            $redata['errno'] = 666;
            $redata['error'] = '添加软件成功';
        } else{
            $redata['errno'] = 600;
            $redata['error'] = '添加软件失败';
        }
        echo json_encode($redata);
    }
    
    
    public function del() {
        $redata = $this->httpcode;
        $id = $this->input->post('id');
        $this->db->delete('qbjh_software', ['id' => $id]);
        if ($this->db->affected_rows()) {
            $redata['errno'] = 666;
            $redata['error'] = '删除成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '删除失败';
        }
        echo json_encode($redata);
    }
    
}
